//
//  NSObject+Common.m
//  CarDeal_IOS
//
//  Created by 孙可 on 2018/8/20.
//  Copyright © 2018年 sunke. All rights reserved.
//

#define kHUDQueryViewTag 101

#import "NSObject+Common.h"

@implementation NSObject (Common)

+ (void)showHudTipStr:(NSString *)tipStr{
    if (tipStr && tipStr.length > 0) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:kKeyWindow animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.detailsLabelFont = [UIFont boldSystemFontOfSize:15.0];
        hud.detailsLabelText = tipStr;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1.0];
    }
}
+ (MBProgressHUD *)showHUDQueryStr:(NSString *)titleStr{
    titleStr = titleStr.length > 0? titleStr: @"正在获取数据...";
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:kKeyWindow animated:YES];
    hud.tag = kHUDQueryViewTag;
    hud.labelText = titleStr;
    hud.labelFont = [UIFont boldSystemFontOfSize:15.0];
    hud.margin = 10.f;
    return hud;
}
+ (NSUInteger)hideHUDQuery{
    __block NSUInteger count = 0;
    NSArray *huds = [MBProgressHUD allHUDsForView:kKeyWindow];
    [huds enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
        if (obj.tag == kHUDQueryViewTag) {
            [obj removeFromSuperview];
            count++;
        }
    }];
    return count;
}

//获取当前时间戳  （以毫秒为单位）

+(NSString *)getNowTimeTimestamp3{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SSS"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];
    
    return timeSp;
    
}


+(NSString *)getRndNumCode{
    NSString * t = [self getNowTimeTimestamp3];
    long nowT = [t longLongValue]*1000L+ arc4random() % 1000;
    return [NSString stringWithFormat:@"%ld",nowT];
}

+(NSString*)getCurrentTimes{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    NSLog(@"currentTimeString =  %@",currentTimeString);
    
    return currentTimeString;
    
}

+(NSString*)getCurrentDay{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"YYYY-MM-dd"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    NSLog(@"currentTimeString =  %@",currentTimeString);
    
    return currentTimeString;
    
}

-(UIActivityIndicatorView *)getIndicatorView{
   UIActivityIndicatorView * uiIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    uiIndicator.center =  CGPointMake(kScreen_Width/2,kScreen_Height/2);
    [uiIndicator setHidesWhenStopped:YES]; //当旋转结束时隐藏
    //     _uiIndicator.color = [UIColor redColor]; // 改变圈圈的颜色为红色； iOS5引入
    return uiIndicator;
}


-(UILabel *)getTitleLabel:(BOOL)isFit{
    UILabel * label = [[UILabel alloc] init];
    [label setAdjustsFontSizeToFitWidth:isFit];
    label.textColor = title_color;
    label.font = textFont;
    return label;
}

-(UIView *)getLineView{
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = line_color;
    return view;
}

-(NSString *)getNotNilString:(NSString *) str{
    if(str ==nil){
        return @"暂无";
    }
    return str;
}

-(NSString *) getListString:(NSArray *) array :(NSString *)propertyName{
    NSString * str = @"";
    if(array!=nil&&array.count>0){
        for(int i = 0; i < array.count ;i++){
            NSObject * object = array[i];
            NSString * value = [object valueForKey:propertyName];
            if(i == array.count -1){
                str = [NSString stringWithFormat:@"%@%@",str,value];
            }else{
                 str = [NSString stringWithFormat:@"%@%@%@",str,value,@","];
            }
        }
    }
    return str;
    
}

@end
